﻿using GuanYijia.Application.CustomerManagement.CustomerDTO;
using GuanYijia.Application.SupplierManagement.SupplierDTO;
using GuanYijia.DoMain;
using GuanYijia.DoMain.PageDto;
using GuanYijia.Repository;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Panda.DynamicWebApi;
using Panda.DynamicWebApi.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GuanYijia.Application.CustomerManagement
{
    [DynamicWebApi]
    [ApiExplorerSettings(GroupName = "v1")]
    public class CustomerDataService : IDynamicWebApi
    {
        private readonly IBaseRepository<Customer> _customerRepository;

        public CustomerDataService(IBaseRepository<Customer> customerRepository)
        {
            _customerRepository = customerRepository;
        }
        [HttpPost]
        public async Task<PageResultDto<CustomerRequisitionOutPut>> GetCustomerShow(CustomerRequisitionInPut customerinput)
        {
            var list = new PageResultDto<CustomerRequisitionOutPut>() { TotalCount = 0, TotalPage = 0, Data = new List<CustomerRequisitionOutPut>() };
            var res = from a in _customerRepository.GetQueryable(tracking: false)
                      select new CustomerRequisitionOutPut
                      {
                          customerid = a.customerid,
                          customer = a.customer,
                          contacts = a.contacts,
                          telephone = a.telephone,
                          phone_num = a.phone_num,
                          email = a.email,
                          begin_need_get = a.begin_need_get,
                          begin_need_pay = a.begin_need_pay,
                          tax_rate = a.tax_rate,
                          sort = a.sort,
                          enabled = a.enabled
                      };
            if (!string.IsNullOrEmpty(customerinput.customer))
            {
                res = res.Where(x => x.customer.Contains(customerinput.customer));
            }
            if (!string.IsNullOrEmpty(customerinput.telephone))
            {
                res = res.Where(x => x.telephone.Contains(customerinput.telephone));
            }
            if (!string.IsNullOrEmpty(customerinput.phone_num))
            {
                res = res.Where(x => x.phone_num.Contains(customerinput.phone_num));
            }
            list.TotalCount = res.Count();
            list.TotalPage = (int)Math.Ceiling(list.TotalCount * 1.0 / customerinput.pageSize);
            list.Data = await res.OrderBy(x => x.sort).Skip((customerinput.pageIndex - 1) * customerinput.pageSize).Take(customerinput.pageSize).ToListAsync();
            return list;
        }
        /// <summary>
        /// 逻辑单删
        /// </summary>
        /// <param name="supplierId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<int> DeleteCustomer(int customerid)
        {
            var requisition = await _customerRepository.GetAsync(customerid);
            requisition.delete_flag = true;
            return await _customerRepository.UpdateAsync(requisition);
        }
        /// <summary>
        /// 逻辑批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> RangeDeleteCustomer(List<int> ids)
        {
            var requisitionList = await _customerRepository.GetAllAsyncList(x => ids.Contains(x.customerid));
            foreach (var item in requisitionList)
            {
                item.delete_flag = true;
            }
            return await _customerRepository.UpdateRangeAsync(requisitionList);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddCustomer(Customer customer)
        {
            var list = _customerRepository.GetQueryable(tracking: false);
            if (list.Where(x => x.customer == customer.customer).Count() > 0)
            {
                return -1;
            }
            return await _customerRepository.InsertAsync(customer);
        }
        /// <summary>
        /// 启用
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateCustomerCanenable(List<int> ids)
        {
            var requisitionList = await _customerRepository.GetAllAsyncList(x => ids.Contains(x.customerid));
            foreach (var item in requisitionList)
            {
                item.enabled = false;
            }
            return await _customerRepository.UpdateRangeAsync(requisitionList);
        }
        /// <summary>
        /// 禁用
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateCustomerDisable(List<int> ids)
        {
            var requisitionList = await _customerRepository.GetAllAsyncList(x => ids.Contains(x.customerid));
            foreach (var item in requisitionList)
            {
                item.enabled = true;
            }
            return await _customerRepository.UpdateRangeAsync(requisitionList);
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="customerid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Customer> GetCustomerInfo(int customerid)
        {
            return await _customerRepository.GetAsync(customerid);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateCustomer(Customer customer)
        {
            var list = _customerRepository.GetQueryable(tracking: false);
            if (list.Where(x => x.customer == customer.customer).Count() > 0)
            {
                return -1;
            }
            return await _customerRepository.UpdateAsync(customer);
        }
    }
}
